package pt.compflow.client.api;

import org.restlet.data.MediaType;
import org.restlet.data.Status;
import org.restlet.representation.EmptyRepresentation;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.Get;

import pt.compflow.Context;
import pt.compflow.flowontology.model.FLOWO;
import pt.compflow.flowontology.model.FlowOntologyModel;
import pt.compflow.flowontology.model.Person;
import pt.compflow.storage.TripleStoreConnection;

public class PersonResource extends APIBaseResource {

//	private static final Logger LOG = LoggerFactory.getLogger(PersonResource.class);
	
	@Get("json")
	public Representation get() {
		Context context = getCompFlowContext();
		TripleStoreConnection store = context.getStoreConnection();
		store.openStore();
		
		FlowOntologyModel model = store.connectModel();
		
		Person p = getPerson(model, null);
		if(p == null) {
			setStatus(Status.CLIENT_ERROR_UNAUTHORIZED);
			store.closeStore();
			return new EmptyRepresentation();
		}		
		
		String uri = FLOWO.getURI() + getRequestAttributes().get("id");
		Person person = model.getPersonWithURI(uri);
		if (person == null) {
			setStatus(Status.CLIENT_ERROR_NOT_FOUND);
			store.closeStore();
			return new EmptyRepresentation();
		} 
		
		StringRepresentation sr = new StringRepresentation(getPersonJson(model, person).toString(), MediaType.APPLICATION_JSON);
		
		store.closeStore();
					
		return sr;
	}

}
